#include <func.h>

#define MAXLINE 1024
#define MAXCOUNT 10

void print_status(int status)
{
    if(WIFEXITED(status))
        printf("status: %d\n",WEXITSTATUS(status));
    else if(WIFSIGNALED(status))
{
            printf("signal: %d\n", WTERMSIG(status));
#ifdef WCOREDUMP
    if(WCOREDUMP(status))
        printf("core dump\n");
#endif
}
}

void analysis(char cmd[], char* argv[])
{
    int i = 0;
    argv[i] = strtok(cmd, " \n\t");
    while(argv[i] != NULL)
    {
        i++;
        argv[i] = strtok(NULL, " \n\t");
    }
}

int main(void)
{
    char cmd[MAXLINE];
    char* argv[MAXCOUNT];
    int status;
    while(1)
    {
        printf("huzhu >");
        fgets(cmd, MAXLINE, stdin);
        if(memcmp(cmd, "exit", 4) == 0)
            _exit(137);

        pid_t pid = fork();
        switch(pid)
        {
        case -1:
            error(1, errno, "fork");
        case 0:
            //分析命令
            analysis(cmd, argv);
            execvp(argv[0], argv);
        default:
            if(waitpid(pid, &status, 0) == -1)
                error(1, errno, "waitpid");
            print_status(status);
        }
    }

    return 0;
}